package dao

import (
	"go-frame/src/server-admin/models"

	"gorm.io/gorm"
)

type UserDAO struct {
	DB *gorm.DB
}

func NewUserDAO(db *gorm.DB) *UserDAO {
	return &UserDAO{DB: db}
}

func (dao *UserDAO) CreateUser(user *models.User) error {
	return dao.DB.Create(user).Error
}

func (dao *UserDAO) GetUserByUsername(username string) (*models.User, error) {
	var user models.User
	err := dao.DB.Where("username = ?", username).First(&user).Error
	return &user, err
}

func (dao *UserDAO) GetUserByID(id uint) (*models.User, error) {
	var user models.User
	err := dao.DB.First(&user, id).Error
	return &user, err
}

func (dao *UserDAO) GetUserByUUID(uuid string) (*models.User, error) {
	var user models.User
	result := dao.DB.Where("uuid = ?", uuid).First(&user)
	if result.Error != nil {
		return nil, result.Error
	}
	return &user, nil
}
